﻿using System.Collections.Generic;
using System.Linq;

using MSAcademic.v2.Database;
using MSAcademic.v2.Infrastructure.Utility;

namespace MSAcademic.v2.Infrastructure.User.Impl
{
    public class DatabaseUserQueries : IUserQueries
    {
        public IUser TryGetWhereUsername(string username)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var user = db.Users.SingleOrDefault(x => x.Username == username && !x.IsHidden);
                if (user == null)
                {
                    return null;
                }
                return new DatabaseUser
                {
                    Id = user.Id,
                    Username = user.Username,
                    Password = user.Password,
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    Description = user.Description,
                    Email = user.Email,
                    IsHidden = user.IsHidden,
                    ActingRole = (ActingRole)user.ActingRole,
                    FacebookLink = user.FacebookLink,
                    TwitterLink = user.TwitterLink,
                    LinkedInLink = user.LinkedInLink,
                    OtherLink = user.OtherLink,
                    UserRightsCsv = user.UserRightsCsv,
                    ProfilePictureUrl = user.ProfilePictureUrl
                };
            }
        }

        public IUser TryGetWhereUsernameAndPassword(string username, string password)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var hashedPassword = PasswordHashService.CreatePasswordHash(password);
                var user = db.Users.SingleOrDefault(x => (x.Username == username && x.Password == hashedPassword && !x.IsHidden));
                if (user == null)
                {
                    return null;
                }
                return new DatabaseUser
                {
                    Id = user.Id,
                    Username = user.Username,
                    Password = user.Password,
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    Description = user.Description,
                    Email = user.Email,
                    IsHidden = user.IsHidden,
                    ActingRole = (ActingRole)user.ActingRole,
                    FacebookLink = user.FacebookLink,
                    TwitterLink = user.TwitterLink,
                    LinkedInLink = user.LinkedInLink,
                    OtherLink = user.OtherLink,
                    UserRightsCsv = user.UserRightsCsv,
                    ProfilePictureUrl = user.ProfilePictureUrl
                };
            }
        }

        public IUser TryGetWhereId(int id)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var user = db.Users.SingleOrDefault(x => x.Id == id && !x.IsHidden);
                if (user == null)
                {
                    return null;
                }
                return new DatabaseUser
                {
                    Id = user.Id,
                    Username = user.Username,
                    Password = user.Password,
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    Description = user.Description,
                    Email = user.Email,
                    IsHidden = user.IsHidden,
                    ActingRole = (ActingRole)user.ActingRole,
                    FacebookLink = user.FacebookLink,
                    TwitterLink = user.TwitterLink,
                    LinkedInLink = user.LinkedInLink,
                    OtherLink = user.OtherLink,
                    UserRightsCsv = user.UserRightsCsv,
                    ProfilePictureUrl = user.ProfilePictureUrl
                };
            }
        }

        public IUser GetArticleAuthor(int articleId)
        {
            using(var db= new MSAcademicDatabaseContainer())
            {
                var article = db.Articles.SingleOrDefault(m => m.Id == articleId);
                if (article == null) return null;

                var author = db.Users.SingleOrDefault(m => m.Id == article.UserId && !m.IsHidden);
                if (author == null) return null;
                return new DatabaseUser
                           {
                               ActingRole = (ActingRole)author.ActingRole,
                               Description = author.Description,
                               Email = author.Email,
                               FacebookLink = author.FacebookLink,
                               FirstName = author.FirstName,
                               Id = author.Id,
                               IsHidden = author.IsHidden,
                               LastName = author.LastName,
                               LinkedInLink = author.LinkedInLink,
                               OtherLink = author.OtherLink,
                               UserRightsCsv = author.UserRightsCsv,
                               ProfilePictureUrl = author.ProfilePictureUrl,

                           };
            }
        }

        public IUser GetWhereEmail(string email)
        {
            using(var db= new MSAcademicDatabaseContainer())
            {
                var user = db.Users.SingleOrDefault(m => m.Email == email);
                if (user == null) return null;

                return new DatabaseUser
                {
                    Id = user.Id,
                    Username = user.Username,
                    Password = user.Password,
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    Description = user.Description,
                    Email = user.Email,
                    IsHidden = user.IsHidden,
                    ActingRole = (ActingRole)user.ActingRole,
                    FacebookLink = user.FacebookLink,
                    TwitterLink = user.TwitterLink,
                    LinkedInLink = user.LinkedInLink,
                    OtherLink = user.OtherLink,
                    UserRightsCsv = user.UserRightsCsv,
                    ProfilePictureUrl = user.ProfilePictureUrl
                };
            }
        }

        public IEnumerable<int> GetUserRights(int id)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var user = db.Users.SingleOrDefault(x => x.Id == id && !x.IsHidden);

                if (user == null) { return null; }

                if (string.IsNullOrEmpty(user.UserRightsCsv)) { return null; }

                var userRights = user.UserRightsCsv.Split(';').Select(int.Parse);
                return userRights;
            }
        }
    }
}
